﻿//收入统计新
"use strict"

exports.index = async function(){
    this.view='branch/income';
    let branch=this.branch;
    //拉出服务器
    let server = this.model.branch("server",branch);
    let keys = ["name","status"]
    let ret= await server.mget(keys);
    let data={server:[]};
    let query_list=[];
    for(let k in ret){
        let v = ret[k];
        let d={};
        if(v._id>=900){
            continue;
        }
        if(v.status==9){
            continue;
        }
        d.sid=v._id;
        d.sname=v.name;
        data.server.push(d);
        try{
            let _body={};
            _body=Object.assign(_body,this.req.body);
            query_list.push(this.library("game/request",branch,v._id,'/income/query',_body)); 

        }catch(err){

        }
    }

   let rets=await Promise.all(query_list);
    for(let i=0;i<rets.length;++i){
        if(rets[i]){
            data.server[i].coll=rets[i];
            // data.server[i]=Object.assign(data.server[i],rets[i]);
        }
        else{
            data.server[i].coll=[];
//            data.server[i]=Object.assign(data.server[i],{time:0,income:0});
        }
    }

    // console.log('======BobFindMe======');
    // console.log(JSON.stringify(data));

    data['bpath'] = ['', 'b', branch].join('/');

    data['setting']={};
    let setting = this.model.admin('setting');
    let setting_ret=await setting.find({});
    if(setting_ret){
        data.setting=Object.assign(data.setting,setting_ret);        
    }

    data['auth']={};
    let auth = this.model.admin('authorize');
    let auth_ret=await auth.find({"uid":this.session.uid});
    if(auth_ret){
        data.auth=Object.assign(data.auth,auth_ret);        
    }

    

    return data;
}

exports.query = function(){
    this.output = 'json';
    let sid = this.get('sid','int');
    if(!sid){
        return this.error('args empty')
    }
    let branch = this.branch;
    return this.library("game/request",branch,sid,'/income/query',this.req.body);
}
